package display;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import Util.Util;
import book.Book;
import orders.Publisher;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JTable;

import exception.InvalidEditionException;

import quantity.Quantity;
import quantity.QuantityList;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.ScrollPane;
import java.awt.Dimension;
import java.sql.SQLException;

public class InventoryQuery extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTable table;
	private JScrollPane tableScroll;
	private InvKeyListener kl;
	
	private InventoryData inv;
	private JLabel lblAuthor;
	private JLabel lblTitle;
	private JLabel lblEd;
	private JLabel lblSalePrice;

	/**
	 * Create the frame.
	 */
	public InventoryQuery(InventoryData i) {
		
		inv = i;
		initialize();
		addContents();
	}
	
	private void initialize() {
		setTitle("Inventory Query");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 550, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{40, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 29, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
	}
	
	private void addContents() {
		
		textField = new JTextField();
		textField.addKeyListener(kl = new InvKeyListener());
		
		JLabel lblIsbn = new JLabel("ISBN");
		GridBagConstraints gbc_lblIsbn = new GridBagConstraints();
		gbc_lblIsbn.insets = new Insets(0, 0, 5, 5);
		gbc_lblIsbn.anchor = GridBagConstraints.EAST;
		gbc_lblIsbn.gridx = 1;
		gbc_lblIsbn.gridy = 0;
		contentPane.add(lblIsbn, gbc_lblIsbn);
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridwidth = 3;
		gbc_textField.insets = new Insets(0, 0, 5, 5);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 2;
		gbc_textField.gridy = 0;
		contentPane.add(textField, gbc_textField);
		textField.setColumns(10);
		
		lblAuthor = new JLabel("Author");
		GridBagConstraints gbc_lblAuthor = new GridBagConstraints();
		gbc_lblAuthor.insets = new Insets(0, 0, 5, 5);
		gbc_lblAuthor.gridx = 0;
		gbc_lblAuthor.gridy = 2;
		contentPane.add(lblAuthor, gbc_lblAuthor);
		
		lblTitle = new JLabel("Title");
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.insets = new Insets(0, 0, 5, 5);
		gbc_lblTitle.gridx = 2;
		gbc_lblTitle.gridy = 2;
		contentPane.add(lblTitle, gbc_lblTitle);
		
		lblEd = new JLabel("Ed");
		GridBagConstraints gbc_lblEd = new GridBagConstraints();
		gbc_lblEd.insets = new Insets(0, 0, 5, 5);
		gbc_lblEd.gridx = 4;
		gbc_lblEd.gridy = 2;
		contentPane.add(lblEd, gbc_lblEd);
		
		lblSalePrice = new JLabel("Sale Price");
		GridBagConstraints gbc_lblSalePrice = new GridBagConstraints();
		gbc_lblSalePrice.insets = new Insets(0, 0, 5, 5);
		gbc_lblSalePrice.gridx = 6;
		gbc_lblSalePrice.gridy = 2;
		contentPane.add(lblSalePrice, gbc_lblSalePrice);
		
		table = new JTable();
		table.setFillsViewportHeight(true);
		
		GridBagConstraints gbc_table = new GridBagConstraints();
		gbc_table.gridheight = 2;
		gbc_table.insets = new Insets(0, 0, 5, 0);
		gbc_table.gridwidth = 8;
		gbc_table.fill = GridBagConstraints.BOTH;
		gbc_table.gridx = 0;
		gbc_table.gridy = 4;
		
		DefaultTableModel tableM = (DefaultTableModel)table.getModel();
		
		tableM.setColumnIdentifiers(new String[]{"Qty","Type","Location","Purchase"});
		
		tableScroll = new JScrollPane(table);
		contentPane.add(tableScroll, gbc_table);
	}
	
	private class InvKeyListener extends KeyAdapter {
		
		@Override
		public void keyPressed(KeyEvent arg0) {
			if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
				Util.printStatusString("You Pressed Enter!");
				
				emptyTable();
				
				String isbn = textField.getText();
				Book b = inv.getBook(isbn);
				
				updateLabels(b);
				
				if (b == null) {
					noQty();
					return;
				}
				
				QuantityList ql = inv.qtyLookup(isbn);
				Util.printStatusString("QuantityList for isbn: "+isbn);
				
				if (ql == null || ql.size() == 0)
					noQty();
				else
					for (int i = 0; i < ql.size(); ++i)
						displayQuantity(ql.get(i));
				
				((DefaultTableModel)table.getModel()).fireTableDataChanged();
			}
		}
		
		private void updateLabels(Book b){
			if (b == null) {
				lblAuthor.setText("Author");
				lblTitle.setText("Title");
				lblEd.setText("Ed");
				lblSalePrice.setText("$0.00");
			}
			else {
				lblAuthor.setText(b.getAuthor());
				lblTitle.setText(b.getTitle());
				lblEd.setText(b.getEdition().getEd());
				lblSalePrice.setText(String.format("$%1$.2f",b.getPrice()));
			}
		}
		
		private void emptyTable() {
			for (int i = 0; i < ((DefaultTableModel)table.getModel()).getRowCount(); )
				((DefaultTableModel)table.getModel()).removeRow(i);
		}
		
		private void displayQuantity(Quantity q) {
			
			String[] tmp = new String[]{
					String.valueOf(q.getQty()),
					q.getType().getType(),
					q.getType().getLocation().toString(),
					String.format("$%1$.2f",q.getType().getPrice())};
			((DefaultTableModel)table.getModel()).addRow(tmp);
			
		}
		
		private void noQty() {
			((DefaultTableModel)table.getModel()).addRow(new String[]{"","None"});
		}
		
	}

}
